package com.xingdata.jjxc.favorites;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.xingdata.jjxc.App;
import com.xingdata.jjxc.Common;
import com.xingdata.jjxc.R;
import com.xingdata.jjxc.base.Base1Activity;
import com.xingdata.jjxc.enty.RespEntity;
import com.xingdata.jjxc.http.HttpResponseListener;
import com.xingdata.jjxc.utils.JUtils;
import com.xingdata.jjxc.utils.SystemInfo;

/**
 * 收藏夹
 * 
 * @author ysc
 * 
 */
public class FavoritesActivity extends Base1Activity {
	
	final static String TAG = "FavoritesActivity";

	/** 返回按钮 */
	private ImageView title_back;

	/** 收藏夹listview */
	private ListView favorites_listview;

	/** adapter */
	private FavoritesAdapter favoritesAdapter;

	/** 收藏夹实体列表 */
	private List<FavoritesEntity> favoritesEntityList = new ArrayList<FavoritesEntity>();

	/** 收藏夹实体 */
	private FavoritesEntity favoritesEntity;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		doGet_findCollectionsbyUserid();
	}

	@Override
	protected int getContentViewId() {
		// TODO Auto-generated method stub
		return R.layout.favorites;
	}

	@Override
	protected String getTitleText() {
		// TODO Auto-generated method stub
		return "收藏夹";
	}

	@Override
	protected void initViews() {
		// TODO Auto-generated method stub
		// 修改字体
		Typeface font = Typeface.createFromAsset(getAssets(), JUtils.TypefacePath);

		title_back = (ImageView) findViewById(R.id.title_back);
		title_back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		
		ImageView title_delet = (ImageView) findViewById(R.id.title_delet);
		title_delet.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				setResult(RESULT_OK);
				finish();
			}
		});

		/* 收藏夹listview初始化 */
		favorites_listview = (ListView) findViewById(R.id.favorites_listview);

		// testAdapter = new TestAdapter(this, favoritesEntityList);
		// favorites_listview.setAdapter(testAdapter);

		favoritesAdapter = new FavoritesAdapter(FavoritesActivity.this, R.layout.favorites_listview_item, favoritesEntityList);
		favorites_listview.setAdapter(favoritesAdapter);

		favorites_listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				// TODO Auto-generated method stub
				favoritesEntity = favoritesEntityList.get(position);
				Log.d(TAG, "点击收藏夹获取某一行实体信息="+favoritesEntity);
				Log.d(TAG, "collectid="+favoritesEntity.getCollect_Id());
				Log.d(TAG, "点击收藏夹获取某一行实体信息="+favoritesEntity.getPlace_name());
				
				Intent intent = new Intent();

				Bundle bundle = new Bundle();
				bundle.putSerializable(Common.FAVORITES_POI, favoritesEntity);// 传值到地图界面，在地图上显示收藏地点的位置
				bundle.putSerializable(Common.PLACE_NAME_INFAVORITES, favoritesEntity);// 传所有相关值到设置家和公司地址的界面
				intent.putExtras(bundle);

				setResult(RESULT_OK, intent);

				finish();
			}
		});
	}
	
	private List<String> collect_IdList = new ArrayList<String>();

	/** 向服务器发送消息，获取收藏夹地址 */
	private void doGet_findCollectionsbyUserid() {
		paramsMap = new HashMap<String, String>();

		paramsMap.put("user_id", SystemInfo.getUserInfo().getUserid());

		mHttpUtil.Post(App.ZZD_REQUEST_FINDCOLLECTIONSBYUSERID, paramsMap, new HttpResponseListener() {

			@Override
			public void onSuccess(RespEntity respEntity) {
				Log.d(TAG,"JSON.result="+JSON.parseArray(respEntity.getResult()));
				favoritesEntityList.addAll(JSON.parseArray(respEntity.getResult(), FavoritesEntity.class));
				favoritesAdapter.notifyDataSetChanged();
				
//				if (FavoriteJudge.isExist("114.086956", "22.574427")) {
//					Log.d(TAG, "cunzai");
//				}
//				for (FavoritesEntity favoritesEntity : favoritesEntityList) {
//					String Lng = favoritesEntity.getPlace_lng();
//					String Lat = favoritesEntity.getPlace_lat();
//					Lnglist.add(Lng);
//					Latlist.add(Lat);
//				}
//				
//				if (Lnglist != null && Latlist != null) {
//					String string = "22.574427";
//					for (String Lat : Latlist) {
//						Log.d(TAG, Lat);
//						if (Lat.equals(string)) {
//							Log.d(TAG, "存在="+Lat);
//						}
//					}
//					
//					for (String Lng : Lnglist) {
//						Log.d(TAG, Lng);
//						if (Lng.equals("114.086956")) {
//							Log.d(TAG, "存在=="+Lng);
//						}
//					}
//				}
			}

			@Override
			public void onLoading(int vurrent) {

			}

			@Override
			public void onFailure(String result) {

			}
		});
	}

}
